package com.chuiyuan.study;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by chuiyuan on 2/12/16.
 */
public class RegMatches {
    public static void  main (String [] args){
        study12();
    }

    /**
     * same as study12
     */
    private static void study13(){
        Pattern p = Pattern.compile("(.*)(\\()(.*)(\\))");
        Matcher m = p.matcher("行宫(元稹)");
        if (m.find()){
            System.out.println(m.group(1));
            System.out.println(m.group(3));
        }
    }
    /**
     *input : xxx(yyy)
     * output :
     * xxx
     * yyy
     */
    private static void study12(){
        String str = "登鹳雀楼(王之涣)";
        String [] strArray = Pattern.compile("\\(|\\)").split("登鹳雀楼(王之涣)");
        //String [] strArray =str.split("\\(||\\)") ;
        System.out.println(strArray.length);
        System.out.println(strArray[0]);
        System.out.println(strArray[1]);
    }

    private static void study11(){
        Pattern p = Pattern.compile("(?<=\\()(.+?)(?=\\))");
        String s = "行宫(元稹)";
        Matcher m = p.matcher(s);
        while (m.find()){
            System.out.println(m.group());
        }
    }

    private static void study10(){
        //提取英文（）内的内容
        Pattern pattern = Pattern.compile("（(.*?)）"); //中文括号
        String s = "行政专员（袁文全）"; //中文括号
        String s1 = "行宫(元稹)";//英语括号,copied,not ok
        String s2 = "行宫（元稹）";//中文括号,ok
        //说明从网页上提取出来的是英文括号
        Matcher matcher = pattern.matcher(s2);
        if (matcher.find()){
            System.out.println(matcher.group(1));
        }
    }
    private static void study9(){
        Pattern p = Pattern.compile("(（.*?）)");
        Matcher m = p.matcher("行宫(元稹)");
        if (m.find()){
            System.out.println(m.group());
        }
    }
    private static void study8(){
        Pattern p = Pattern.compile("\\d+");
        Matcher m = p.matcher("我的QQ是:456456 我的电话是:0532214 我的邮箱是:aaa123@aaa.com");
        while (m.find()){
            System.out.println(m.group());
        }
    }
    private static void study7(){
        Pattern p = Pattern.compile("([a-z]+)(\\d+)");//2 groups
        Matcher m = p.matcher("aaa2223bb444");
        System.out.println(m.groupCount());//2
        while (m.find()){
            System.out.println("====one match");
            //all
            System.out.print(m.start(0)+",");//0
            System.out.print(m.end(0)+",");   //7
            System.out.println(m.group(0));  //aaa2223
            //group 1
            System.out.print(m.start(1)+",");//0
            System.out.print(m.end(1)+",");   //3
            System.out.println(m.group(1));  //aaa
            //goup 2
            System.out.print(m.start(2)+",");//3
            System.out.print(m.end(2)+",");  //7
            System.out.println(m.group(2));//2223
        }


    }
    private static void study6(){
        Pattern p = Pattern.compile("\\d+");
        Matcher m = p.matcher("aaaa2222bb");
        m.find();//
        System.out.println(m.start());//4
        System.out.println(m.end());  //8
        System.out.println(m.group());//2222

        Matcher m2 = p.matcher("2222bb");
        m2.lookingAt();
        System.out.println(m2.start());//0
        System.out.println(m2.end());  //4
        System.out.println(m2.group());//222

        Matcher m3 = p.matcher("222");
        m3.matches();
        System.out.println(m3.start());//0
        System.out.println(m3.end());  //3
        System.out.println(m3.group());//222
    }

    /**
     *  Matcher 类也提供了appendReplacement 和appendTail 方法用于文本替换
     */
    private static void study5(){
        String reg = "a*b";
        String input ="aabfooaabfooabfoob";
        String replace ="-";

        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(input);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()){
            matcher.appendReplacement(sb,replace);
        }
        matcher.appendTail(sb);//? same result without it
        System.out.println(sb.toString());
    }
    /**
     * replaceFirst 和replaceAll 方法用来替换匹配正则表达式的文本。
     * 不同的是，replaceFirst 替换首次匹配，replaceAll 替换所有匹配
     */
    private static void study4(){
        String reg ="dog";
        String input = "The dog says meow. " +
                "All dogs say meow.";
        String replace = "cat";

        Pattern pattern = Pattern.compile(reg);
        //get a matcher object
        Matcher matcher = pattern.matcher(input);

        input = matcher.replaceAll(replace);
        System.out.println(input);
    }

    private static void study1(){
        String line = "2016-02-15";
        String pattern = "(\\d{4})-(\\d{2}-(\\d\\d))";
        //([0-9])=(//d)

        //pattern对象是一个正则表达式的编译表示
        Pattern r = Pattern.compile(pattern);

        //Matcher对象是对输入字符串进行解释和匹配操作的引擎。
        Matcher m = r.matcher(line);
        System.out.println("group:"+ m.groupCount());
        if (m.find()){
            System.out.println("Found value: "+ m.group(0));//all
            System.out.println("Found Value: "+ m.group(1));//(.*)
            System.out.println("Found value: "+ m.group(2));//([0-9])
            System.out.println("Found value: "+ m.group(3));//

        }else {
            System.out.println("No match");
        }
    }

    private static void study2(){
        String reg = "\\bcat\\b";
        String input ="cat cat cat cattie cat";

        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(input);//得到matcher对象
        int count =0;

        while (m.find()){
            count++;
            System.out.println("Matcher number:"+ count);
            System.out.println("start():"+ m.start());
            System.out.println("end():"+ m.end());
        }

    }

    private static void study3(){
        String reg = "foo";
        String input = "foooooo";

        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(input);

        System.out.println("lookingAt():"+matcher.lookingAt());//不要求都匹配
        System.out.println("matchers():"+ matcher.matches()); //要求都匹配
    }
}
